﻿

#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;
using DTO;
#endregion

namespace DAO
{
    public class NhanVienDAO:Dataprovider
    {
         #region Phuong thuc
        public NhanVienDAO()
        {
        }

        // lay danh sach nhan vien.
        public ArrayList getDsNhanVien()
        {
            connect();
            string query = "SELECT * FROM NHANVIEN";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);

            return arr;
        }

        // lay du lieu dua vao dataRow
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            NhanVienDTO nv = new NhanVienDTO();

            nv.MaNhanVien = dt.Rows[i]["MaNhanVien"].ToString();
            nv.TenNhanVien = dt.Rows[i]["TenNhanVien"].ToString();
            nv.LoaiNhanVien = dt.Rows[i]["LoaiNhanVien"].ToString();
            nv.CMND = dt.Rows[i]["CMND"].ToString();
            nv.SoDienThoai = dt.Rows[i]["SoDienThoai"].ToString();
            nv.Luong = (float)dt.Rows[i]["Luong"];
            
            return (object)nv;
        }

        public void insert(NhanVienDTO info)
        {
            string insertCommand = "INSERT INTO NHANVIEN VALUES('" +
                info.MaNhanVien + "', '" +
                info.TenNhanVien + "', '" +
                info.LoaiNhanVien + "', '" +
                info.CMND + "', '" +
                info.SoDienThoai + "', " +
                info.Luong + ")";

            executeNonQuery(insertCommand);
        }

        public void update(NhanVienDTO info)
        {
            string updateCommand = "UPDATE NHANVIEN " +
                                    "SET TenNhanVien = '" + info.TenNhanVien + "', " +
                                    " LoaiNhanVien = '" + info.LoaiNhanVien + "', " +
                                    " CMND = '" + info.CMND + "', " +
                                    " SoDienThoai = '" + info.SoDienThoai + "', " +
                                    " Luong = " + info.Luong + ", " +
                                    " WHERE MaNhanVien = '" + info.MaNhanVien + "'";


            executeNonQuery(updateCommand);
        }

        public void delete(string maNV)
        {
            string deleteCommand = "DELETE FROM NHANVIEN WHERE MaNhanVien = '" + maNV + "'";
            executeNonQuery(deleteCommand);
        }
        #endregion
    }
}
